//
//  UIView+MJPlayer.m
//  MJVideoPlayer
//
//  Created by 梅杰 on 16/8/13.
//  Copyright © 2016年 梅杰. All rights reserved.
//

#import "UIImage+MJPlayer.h"
#import <Photos/Photos.h>
#import "UIImageView+WebCache.h"
#import <MobileVLCKit/MobileVLCKit.h>
@implementation UIImage (MJPlayer)
//用来获取某个url视频的缩略图
+ (UIImage*) thumbnailImageForVideo:(NSURL *)videoURL {
    
    //缓存图片的路径
    

//    NSAssert(videoURL, @"url is nil");
    if (!videoURL) {
        NSLog(@"传入的url是空的 UIImage + MJPlayer");
        return nil;
    }
    //AVURLAsset是AVAsset的子类，通过一个本地或远程的url初始化一个AVAsset对象
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
    
    AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    
    gen.appliesPreferredTrackTransform = YES;
    
    CMTime time = CMTimeMakeWithSeconds(2.0, 600);
    
    NSError *error = nil;
    
    CMTime actualTime;
    
    CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
    
    UIImage *thumbImg = [[UIImage alloc] initWithCGImage:image];
    
    UIImage *smallImg = [self thumbnailWithImage:thumbImg size:CGSizeMake(100, 100)];
    
    return smallImg;
    
}

+ (UIImage *)thumbnailWithImage:(UIImage *)image size:(CGSize)asize

{
    
    UIImage *newimage;
    
    
    if (nil == image) {
        
        newimage = nil;
        
    }
    
    else{
        
        UIGraphicsBeginImageContext(asize);
        
        [image drawInRect:CGRectMake(0, 0, asize.width, asize.height)];
        
        newimage = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
        
    }
    
    return newimage;
    
}

//这个函数中存储图片并返回它的url
+ (NSString *)localUrlWithImageWithVideoUrl:(NSURL *)VideoUrl{
    NSArray *cacheArray = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachePath = cacheArray[0];
    //图片文件夹路径
    NSString *thumbPath = [cachePath stringByAppendingPathComponent:@"ThumbImage"];
    //图片路径
    NSString *identString = [[VideoUrl absoluteString] lastPathComponent];
    if (thumbPath) {
        NSString *filePath = [thumbPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",identString]];
        
        if (![[NSFileManager defaultManager] fileExistsAtPath:thumbPath]) {
//            NSFileManager *fileManager = [[NSFileManager alloc] init];
            NSError *error;
            [[NSFileManager defaultManager] createDirectoryAtPath:thumbPath withIntermediateDirectories:YES attributes:nil error:&error];
            if (error) {
                NSLog(@"%@",[error localizedDescription]);
            }
//            NSLog(@"%d",flag);
        }
        UIImage *image = [self thumbnailImageForVideo:VideoUrl];
        
        if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
            NSString *filePath = [thumbPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",identString]];
//            NSArray *arr = [NSArray arrayWithObjects:@"ni",@"ma",@"le", nil];
//            NSString *fd = [cachePath stringByAppendingPathComponent:@"abc.plist"];
//            BOOL f = [arr writeToFile:fd atomically:YES];
           [UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES];
//            NSLog(@"%d ",flag);
            
        }
        return filePath;
    }else{
        NSLog(@"不能成功创建文件夹 imageView拓展方法");
        return nil;
    }
}

@end
